package com.bw.ods;

import com.alibaba.fastjson.JSON;
import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource;
import com.alibaba.ververica.cdc.connectors.mysql.table.StartupOptions;
import com.alibaba.ververica.cdc.debezium.DebeziumSourceFunction;
import com.bw.util.JsonDeserializationSchemaUtil;
import com.bw.util.MyKafkaUtil;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;

import java.util.Properties;

public class FlinkCDCApp {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DebeziumSourceFunction<String> source = MySQLSource.<String>builder()
                .hostname("hadoop102")
                .port(3306)
                .databaseList("tms01") // set captured database
                //.tableList("yourDatabaseName.yourTableName") // set captured table
                .username("root")
                .password("123456")
                .startupOptions(StartupOptions.initial())
                .deserializer(new JsonDeserializationSchemaUtil()) // converts SourceRecord to JSON String
                .build();

        DataStreamSource<String> stream = env.addSource(source);
        //过滤data为空的数据
        SingleOutputStreamOperator<String> filterDS = stream.filter(x -> JSON.parseObject(x).getJSONObject("data") != null && JSON.parseObject(x).getJSONObject("data").size() > 0);
        filterDS.addSink(MyKafkaUtil.getKafkaProducer("tms_ods_yk6"));
        env.execute();
    }
}
